package com.jarchie.htgl.base;

import android.app.Application;
import android.content.Context;

import com.coder.zzq.smartshow.toast.SmartToast;
import com.jarchie.htgl.R;
import com.jarchie.htgl.bean.UserLogin;
import com.jarchie.htgl.constants.ConfigConstant;
import com.jarchie.htgl.greendao.GreenDaoManager;
import com.jarchie.htgl.utils.SharePreUtils;

/**
 * Created by Jarchie on 2017\12\11.
 * 全局的Application
 */

public class BaseApp extends Application {
    private static Context mContext;
    private static BaseApp mInstance = null;
    private static SharePreUtils mSpUtils;
    private UserLogin mUserLogin;

    public BaseApp() {} //此处私有构造会报错

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        mSpUtils = new SharePreUtils(ConfigConstant.SHARE_NAME);

        //初始化greenDao数据库实例
        GreenDaoManager.getInstance();

        //初始化SmartToast对象，自定义Toast显示
        SmartToast.plainToast(this)
                .backgroundColorRes(R.color.color_main_dark)
                .textColorRes(R.color.color_white)
                .textSizeSp(16)
                .textBold(true);
    }

    //双重校验锁懒汉单例模式返回公共静态本类对象
    public static BaseApp getInstance() {
        if (mInstance == null) {
            synchronized (BaseApp.class) {
                if (mInstance == null) {
                    mInstance = new BaseApp();
                }
            }
        }
        return mInstance;
    }

    //获取全局上下文对象
    public static Context getContext() {
        if (mContext != null)
            return mContext;
        throw new NullPointerException("you should init first!");
    }

    //获取配置文件对象
    public static SharePreUtils getSpUtils() {
        return mSpUtils;
    }

    //保存用户登录信息
    public void setUserLogin(UserLogin userLogin) {
        if (mUserLogin == null) {
            mUserLogin = new UserLogin();
        }
        this.mUserLogin = userLogin;
    }

    public UserLogin getUserLogin() {
        return mUserLogin;
    }

}
